package tech.aistar.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RegisterUtil {
    // 手机号文本框 - 发送验证码 => ajax请求  => ①生成code  ②SmsUtil.sendSms(phone,code)  ③目前session存储(phone,code)

    // 验证码框  => 登录 => ajax提交后台  =>  页面输入的code
//    AccessKey ID: LTAI5tFnRTFax8fxTjMzW1y4
//    AccessKey Secret: 44DOp1TJpiqTfmhANJM9zWHsJZLbZs
    @Value("${aliyun.access.key-id}")
    private static String keyId;
    @Value("${aliyun.access.secret}")
    private static String secret;

    public static void sendSms(String phone,String code) {

        // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","LTAI5tFnRTFax8fxTjMzW1y4","44DOp1TJpiqTfmhANJM9zWHsJZLbZs");
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),       // The AccessKey ID of the RAM account
         System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),   // The AccessKey Secret of the RAM account
         System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN"));     // STS Token
         **/

        IAcsClient client = new DefaultAcsClient(profile);


        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        request.setTemplateCode("SMS_154950909");
//        request.setPhoneNumbers("15046634773");
        request.setPhoneNumbers(phone);
//        request.setTemplateParam("{\"code\":\"12345\"}");
        request.setTemplateParam("{\"code\":\""+code+"\"}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
    public static String Code(){
        return String.valueOf((int)((Math.random() * 9000) + 1000));
    }
}
